/*
 * Copyright 2022-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sagacity.framework.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

/**
 * Json工具类
 * @author xingyun
 * @date 2022/2/2 11:24
 */
@Slf4j
public final class JsonUtil {
    private JsonUtil(){}

    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
        mapper.setDefaultPropertyInclusion(NON_EMPTY);
    }


    public static String toJson(Object value){
        try {
            return mapper.writeValueAsString(value);
        } catch (Exception e) {
            log.error("[JsonUtil#toJson] 异常",e);
        }
        return null;
    }


    public static <T> T jsonToType(String value,Class<T> clazz){
        try {
            return mapper.readValue(value,clazz);
        } catch (Exception e) {
            log.error("[JsonUtil#toJson] 异常",e);
        }
        return null;
    }





}
